//import { paidMemebrShipList,fetchMemberList, fetchMemberDetail, changeMemberStatus, fetchMemberTag, fetchPayRecord, fetchPoint, addTag } from '../services/memberService';
import * as memberService from '../services/memberService';
import { fetchTagList } from '../services/contentService';
import { decryptRes, dealPagination } from '../utils/requestUtil';
import { message } from 'antd';
import * as franService from '../services/franService';

export default {
  namespace: 'member',

  state: {
    memberList: [],
    memberPagination: {},
    memberTag: [],

    memberDetail: null,
    payRecord: null,
    point: null,

    franList: [],
    paidMemberData:[],
    paidPagination:{},
    phoneList:[],
    AssetInfoData:{},
    AssetConfigData:{},
  },

  effects: {
    *fetchMemberTag({ payload }, { call, put }) {
      console.log('前端参数11111121212', payload)
      const response = JSON.parse(decryptRes(yield call(memberService.fetchTagList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'memberTagReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchMemberList({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(memberService.fetchMemberList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'memberListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchMemberDetail({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(memberService.fetchMemberDetail, payload)));
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'memberDetailReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *upMember({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(memberService.changeMemberStatus, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('操作成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }

    },

    *downMember({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(memberService.changeMemberStatus, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('操作成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }

    },

    *fetchPayRecord({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(memberService.fetchPayRecord, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'payRecordReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchPoint({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(memberService.fetchPoint, payload)));
      console.log('接口返回数据fetchPointfetchPoint+----：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'pointReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addTag({ payload, callback }, { call, put }) {
      console.log('addTag前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(memberService.addTag, payload)));
      console.log('addTag接口返回数据：', response);

      if (response.code === 0) {
        message.success('操作成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }

    },

    *fetchFranList({ payload }, { call, put }) {
      console.log('前端参数11111121212', payload)
      const response = JSON.parse(decryptRes(yield call(franService.fetchFranList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'franListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    *paidMemberList({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(memberService.paidMemebrShipList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'paidMemberListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    //会员权益购买
    *sumbitAssetOrders({ payload ,callback}, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(memberService.sumbitAssetOrders, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        message.success('购买成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    //手机号快速查询会员列表
    *getPhoneUserList({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(memberService.getPhoneUserList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
                let data = response.data;
          if(data.length===0){
            message.error('没有此手机号！')
                }else{
                  yield put({
                    type: 'phoneListReducer',
                    payload: { data },
                  });
                }
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    //
    *getConfigureAssetInfo({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(yield call(memberService.getConfigureAssetInfo, payload));
       console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'AssetInfoReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    //会员权益设置
    *configureAssetInfo({ payload ,callback}, { call, put }) {
      console.log('前端参数',payload )
      const response = yield call(memberService.configureAssetInfo, payload);
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        message.success('设置成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    
    //
    *getUserAssetConfigure({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(memberService.getUserAssetConfigure, payload)));
       console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'AssetConfigureReducer',
          payload: { data },
        });
      } else {
        message.error('提醒：' + response.msg);
      }
    },


  },

  reducers: {
    memberTagReducer(state, action) {
      return {
        ...state,
        memberTag: action.payload.data,
      };
    },
    memberListReducer(state, action) {
      console.log(action.payload.data)
      return {
        ...state,
        memberList: action.payload.data.dataList,
        memberPagination: dealPagination(action.payload.data.pageInfo),
      };
    },
    memberDetailReducer(state, action) {
      console.log(action.payload.data)
      return {
        ...state,
        memberDetail: action.payload.data,
      };
    },
    payRecordReducer(state, action) {
      console.log(action.payload.data)
      return {
        ...state,
        payRecord: action.payload.data,
      };
    },

    pointReducer(state, action) {
      console.log('fetchPointfetchPoint',action.payload.data)
      return {
        ...state,
        point: action.payload.data,
      };
    },

    franListReducer(state, action) {
      console.log(action.payload.data)
      return {
        ...state,
        franList: action.payload.data.dataList,
      };
    },
    paidMemberListReducer(state, action) {
      console.log(action.payload.data)
      return {
        ...state,
        paidMemberData: action.payload.data.list,
        paidPagination:{
          pageSize:action.payload.data.pageSize,
          pageNum:action.payload.data.pageNum,
          pages:action.payload.data.pages,
          size:action.payload.data.size,
          total:action.payload.data.total,
        }
      };
    },
    phoneListReducer(state, action) {
      console.log(action.payload.data)
      return {
        ...state,
        phoneList: action.payload.data,
      };
    },
    AssetInfoReducer(state, action) {
      console.log(action.payload.data)
      return {
        ...state,
        AssetInfoData: action.payload.data,
      };
    },
    AssetConfigureReducer(state, action) {
      console.log(action.payload.data)
      return {
        ...state,
        AssetConfigData: action.payload.data,
      };
    },
  },
};
